<template>
  <!-- 商品橱窗对话框 -->
  <div class="shelves">
    <el-dialog
      title="提示"
      :visible.sync="isShelves"
      width="20%"
      :before-close="cancel">
      <div class="shelves-content">
        <div>
          <div class="radio-item">
            <el-radio v-model="radio" label="new">立即上架</el-radio>
          </div>
          <div class="radio-item">
            <el-radio v-model="radio" label="timing">定时上架</el-radio>
            <el-date-picker
              v-if="isTiming"
              v-model="time"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="submit()">确认</el-button>
        <el-button @click="cancel">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {batchReverse} from '@/project/service/product'

export default {
  name:'shelves',
  props: {
    isShelves:{
      type: Boolean,
      default: () => {
        return false
      }
    },
    productId:{
      type: String,
      default: () => {
        return ''
      }
    },
  },
  data() {
    return {
      radio: 'new',//选择
      isTiming: false,//用户选择定时
      time: '',//定时所选时间
    }
  },
  watch:{
    // isShelves(val){
    //   if(val){
    //     this
    //   }
    // },
    radio(val){
      if(val=='timing'){
        this.isTiming = true
      }else{
        this.isTiming = false
      }
    }
  },
  methods: {
    //获取当前时间
    getPresentTime(){
      let time =  Date.parse(new Date());
      let date = new Date(time)
      let year = date.getFullYear()
      let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      let hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      let mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      let ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return `${year}-${month}-${day} ${hh}:${mm}:${ss}`
    },
    //提交
    submit(){
      if(this.radio=='timing' && this.time==''){
        this.$message({
          type: 'warning',
          message: '请选择定时上架时间！'
        });
      }else{
        // let sellableTime = this.time ? this.time : this.getPresentTime()
        let param = {
          idList: [this.productId],
          sellable:true,
        }
        if(this.time){
          param.sellableTime = this.time
        }
        console.log(param)
        batchReverse(param,data => {
          if(data == '204'){
            this.$message.success('设置成功')
            this.cancel(true)
          }
        })
      }

    },
    //取消
    cancel(isUpdate){
      this.radio = 'new'
      this.time = ''
      this.$emit('close',isUpdate);
    },
  }
}
</script>

<style lang="less" scoped>
.radio-item{
  margin-top:20px;
}
</style>
